package router

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"path"
	"weekly1/pkg"
	"weekly1/user-client/client"
	"weekly1/user-client/request"
	"weekly1/user-server/protot/user"
)

func UserRegister(c *gin.Context) {
	var data request.User
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "未接收到参数",
			"data": nil,
		})
		return
	}
	register, err := client.UserRegister(c, &user.UserRegisterReq{
		Name:     data.Name,
		Phone:    data.Phone,
		Password: data.Password,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "注册失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "注册成功",
		"data": register,
	})
}

func UserLogin(c *gin.Context) {
	var data request.User
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "未接收到参数",
			"data": nil,
		})
		return
	}
	login, err := client.UserLogin(c, &user.UserLoginReq{
		Name:     data.Name,
		Phone:    data.Phone,
		Password: data.Password,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "注册失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "注册成功",
		"data": login,
	})
}

func Upload(c *gin.Context) {
	file, err := c.FormFile("file")
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "文件未上传",
			"data": nil,
		})
		return
	}
	ext := file.Filename
	path.Ext(ext)
	if ext != ".mp4" && ext != ".vom" {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "上传文件格式不对",
			"data": nil,
		})
		return
	}
	if file.Size > 20&1024*1024*1024 {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "上传文件内存太大",
			"data": nil,
		})
	}
	pkg.ImageOs()
	image := "D:\\GoWork\\zg4\\rk\\weekly1\\user-server\\image\\img\\" + file.Filename
	err = c.SaveUploadedFile(file, image)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "文件上传路径未找到",
			"data": nil,
		})
		return
	}
	url := "/image/img"
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "文件上传成功",
		"data": url,
	})
}

func VideoAdd(c *gin.Context) {
	var data request.Video
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "未接收到参数",
			"data": nil,
		})
		return
	}
	video, err := client.VideoAdd(c, &user.VideoAddReq{
		Name:    data.Name,
		Date:    data.Date,
		Content: data.Content,
		Status:  data.Status,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "上传失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "上传成功",
		"data": video,
	})
}
